//
//  JMDisplayFrame.m
//  OpenSesame
//
//  Created by 张家铭 on 16/1/10.
//  Copyright © 2016年 OpenSesame. All rights reserved.
//

#define nameFont [UIFont systemFontOfSize:15]
#define frequencyFont [UIFont systemFontOfSize:11]
#define fitLabelFont [UIFont systemFontOfSize:14]
#import "JMDisplayFrame.h"
#import "JMCar.h"

@implementation JMDisplayFrame

/**
 *  计算文字尺寸
 *
 *  @param text    需要计算尺寸的文字
 *  @param font    文字的字体
 *  @param maxSize 文字的最大尺寸
 */
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (void)setCar:(JMCar *)car {
    _car = car;
    
    // 设置车钥匙头像
    _iconF = (CGRect){{10,10}, {75, 75}};
    
    // 设置车钥匙名称Label
    CGSize nameSize = [self sizeWithText:self.car.name font:nameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    _keyNameLabelF = (CGRect){{CGRectGetMaxX(_iconF) + 8, 25}, {nameSize.width, 16}};
    
    // 设置频率label
    NSString *frequencyStr = [NSString stringWithFormat:@"%@MHz", self.car.pinlv];
    CGSize frequencySize = [self sizeWithText:frequencyStr font:frequencyFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    _frequencyLabelF = (CGRect){{CGRectGetMaxX(_keyNameLabelF) + 8, _keyNameLabelF.origin.y + 3}, {frequencySize.width, 8}};
    
    // 设置右边说明书
    _specificationF = (CGRect){{JMPhoneWidth - 33, _keyNameLabelF.origin.y}, {13, 15}};
    
    // 设置适配label
    CGSize fitLabelSize = [self sizeWithText:self.car.desurl font:fitLabelFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    _fitLabelF = (CGRect){{_keyNameLabelF.origin.x, CGRectGetMaxY(_keyNameLabelF) + 15}, {fitLabelSize.width, 14}};
    
    // cell的高度
    _cellHeight = 98;
}
@end
